<?php
/**
* All plugin classes must extend from this class.
*
* Copyright (c) 2009 James Gauld <james@jamesgauld.net>
* This file is part of Scribble.
* @license http://www.scribblecms.co.uk/license.txt
*
* @package Scribble
*/
use Buan\Config;
use Buan\UrlCommand;
abstract class ScribblePlugin {

	/**
	* Returns the name of this plugin (ie. the class name).
	*
	* @return string
	*/
	final public function getName() {
		return get_class($this);
	}

	/**
	* Setup the plugin's controllers in order for Buan to pick them up.
	*
	* Each argument after the first in this method is a 2-element array where the
	* first element is the controller name (in lower-hyphenated format), and the
	* second is the type of environment in which that controller should be run.
	*
	* @example
	* $this->setupControllers('/path/to/controllers', array('sp-people', ScribbleEnvironment::ADMIN));
	*
	* @param string Absolute path to folder holding all this plugin's controllers
	* @params mixed See documentation for valid arguments
	* @return void
	*/
	protected function setupControllers($cd) {

		// Store controllers folder
		Config::set("ext.Scribble.plugin.".get_class($this).".dir.controllers", $cd);

		// Set the environment depending on the requested URL, and register custom
		// URL routes for each controller
		$controllers = func_get_args();
		array_shift($controllers);
		foreach($controllers as $controller) {
			list($cs, $env) = $controller;
			$cs = preg_replace("/^\/+/", "", $cs);
			$csq = preg_quote($cs, "/");
			if(!ScribbleEnvironment::isAdmin() && preg_match("/^".$csq."/i", Config::get('app.command.requested'))) {
				ScribbleEnvironment::set(ScribbleEnvironment::ADMIN);
			}
			UrlCommand::addCustomRoute("/^".$csq."/i", $cd);
		}
	}
}
?>